### Reproducing main text figures

#loading required packages ####
library(ggplot2)
library(dplyr)
library(grid)
library(gridExtra)
library(coefplot)
library(Hmisc)
library(psych)
library(GPArotation)
library(here)
summarize <- dplyr::summarize
#set working directory
i_am("reproduce_maintext_figures.R")

## FIGURE 1 ####

#load data
dat <- read.csv(here("comb_traitratings.csv"))
db <- read.csv(here("nc_voters_race.csv"))
dat <- left_join(dat, db)
feat <- c("Emily", "Laurie", "Misty", "Octavia", "Keisha", "Latoya")
dat$select <- ifelse(dat$name %in% feat, 1, 0)

#produce plots for each racial group
a <- ggplot(data=dat) + geom_point(aes(x=pctwhite, y=white)) + geom_smooth(aes(x=pctwhite, y=white), se=FALSE) +
  geom_point(data=dat[dat$select==1,], aes(x=pctwhite, y=white), colour="black", fill="gray", shape=21, size=3) +
  xlab("Percent White in Voter File") + ylab("Rated Likelihood White") + theme_bw() + ylim(c(1.5,4.5))
b <- ggplot(data=dat) + geom_point(aes(x=pctblack, y=African.American)) + geom_smooth(aes(x=pctblack, y=African.American), se=FALSE) +
  geom_point(data=dat[dat$select==1,], aes(x=pctblack, y=African.American), colour="black", fill="gray", shape=21, size=3) +
  xlab("Percent Black in Voter File") + ylab("Rated Likelihood Black") + theme_bw() + ylim(c(1.5,4.5))
c <- ggplot(data=dat) + geom_point(aes(x=pcthisp, y=Hispanic)) + geom_smooth(aes(x=pcthisp, y=Hispanic), se=FALSE) +
  geom_point(data=dat[dat$select==1,], aes(x=pcthisp, y=Hispanic), colour="black", fill="gray", shape=21, size=3) +
  xlab("Percent Hispanic in Voter File") + ylab("Rated Likelihood Hispanic") + theme_bw() + ylim(c(1.5,4.5))
d <- ggplot(data=dat) + geom_point(aes(x=pctapi, y=Asian)) + geom_smooth(aes(x=pctapi, y=Asian), se=FALSE) +
  geom_point(data=dat[dat$select==1,], aes(x=pctapi, y=Asian), colour="black", fill="gray", shape=21, size=3) +
  xlab("Percent Asian in Voter File") + ylab("Rated Likelihood Asian") + theme_bw() + ylim(c(1.5,4.5))

#combine
pdf("Figures/figure1.pdf")
grid.arrange(a,b,c,d, nrow=2, top=textGrob("Rated Race of Names by Actual Race Distribution", gp=gpar(fontsize=17)))
dev.off()

#clear environment
rm(list=ls())

## FIGURE 2 ####

dat <- read.csv(here("comb_traitratings.csv"))
datb <- read.csv(here("nameratings.csv"))
dat <- merge(dat, select(datb, name, race, gender, distinct))
datb <- read.csv(here("comb_traitratings_withses.csv"))
dat <- merge(dat, select(datb[datb$att=="competent",], Std..Error, name))

names <- dat %>% 
  filter(race=="black"&!is.na(competent)) %>% 
  slice_min(competent, n=3) %>% 
  arrange(competent) %>%
  select(name) %>%
  bind_rows(dat %>% 
              filter(race=="black"&!is.na(competent)) %>% 
              slice_max(competent, n=3) %>% 
              arrange(competent) %>%
              select(name)) %>%
  bind_rows(dat %>% 
              filter(race=="white"&!is.na(competent)) %>% 
              slice_min(competent, n=3) %>% 
              arrange(competent) %>%
              select(name)) %>%
  bind_rows(dat %>% 
              filter(race=="white"&!is.na(competent)) %>% 
              slice_max(competent, n=3) %>% 
              arrange(competent) %>%
              select(name)) %>%
  bind_rows(dat %>% 
              filter(race=="hispanic"&!is.na(competent)) %>% 
              slice_min(competent, n=3) %>% 
              arrange(competent) %>%
              select(name)) %>%
  bind_rows(dat %>% 
              filter(race=="hispanic"&!is.na(competent)) %>% 
              slice_max(competent, n=3) %>% 
              arrange(competent) %>%
              select(name)) %>%
  bind_rows(dat %>% 
              filter(race=="asian"&!is.na(competent)) %>% 
              slice_min(competent, n=3) %>% 
              arrange(competent) %>%
              select(name)) %>%
  bind_rows(dat %>% 
              filter(race=="asian"&!is.na(competent)) %>% 
              slice_max(competent, n=3) %>% 
              arrange(competent) %>%
              select(name))
names <- unlist(names[[1]])

a <- dat %>%
  filter(name %in% names) %>%
  mutate(name = factor(name, levels=names)) %>%
  arrange(competent) %>%
  ggplot(aes(x=competent, y=name)) + theme_bw() +
  geom_point() +
  geom_errorbarh(aes(xmin=competent - 1.96*Std..Error, 
                     xmax=competent + 1.96*Std..Error),
                 height=.2) +
  xlim(c(2,4.3)) +
  facet_wrap(~capitalize(race), nrow = 1,
             scales="free") +
  xlab("Competence Rating") + ylab("Name")

pdf("Figures/figure2.pdf", width=10, height=3)
a
dev.off()


## FIGURE 3 ####
dat <- read.csv(here("mturk_nametraits_withSEs.csv"))
colnames(dat) <- c("Estimate", "Std..Error", "att", "name")

#select names and attributes for figure
names4fig <- c("Emily", "Laurie", "Latoya", "Keisha", "Octavia", "Misty")
atts4fig <- c("white", "African American", "competent", "hardworking", "intelligent", "professional", "warm", "working class")
subtable2 <- dat[(dat$name %in% names4fig)&(dat$att %in% atts4fig),]

#reformat for plot 
subtable2$att <- capitalize(subtable2$att)
subtable2$att <- factor(subtable2$att, levels=rev(c("White", "white", "African American", "african american",
                                                    "Competent", "competent", "Hardworking", "hardworking",
                                                    "Intelligent", "intelligent", "Professional", 
                                                    "professional", "Warm", "warm", "Working class", "working class")))
subtable2$name <- factor(subtable2$name, levels=c("Emily", "Laurie", "Misty", "Latoya", "Keisha", "Octavia"))
subtable2 <- with(subtable2, subtable2[order(-as.numeric(name)),])

#select final subset for plot
white <- c("Emily", "Laurie", "Misty")
black <- c("Octavia", "Latoya", "Keisha")

#create plot
a <- ggplot() + 
  geom_errorbarh(data=subtable2[subtable2$name %in% white,],
                 aes(xmin=(Estimate-1.96*`Std..Error`), 
                     xmax=(Estimate+1.96*`Std..Error`), y=att, fill=name), 
                 colour="gray69", height=.5,
                 position=position_dodgev(height=-.5)) +
  geom_errorbarh(data=subtable2[subtable2$name %in% black,],
                 aes(xmin=(Estimate-1.96*`Std..Error`), 
                     xmax=(Estimate+1.96*`Std..Error`), y=att, fill=name), 
                 colour="gray69", height=.5,
                 position=position_dodgev(height=-.5)) +
  geom_point(data=subtable2[subtable2$name %in% white,],
             aes(x=Estimate, y=att, shape=name), size=3,
             position=position_dodgev(height=-.5)) +
  geom_point(data=subtable2[subtable2$name %in% black,],
             aes(x=Estimate, y=att, shape=name), size=3,
             position=position_dodgev(height=-.5)) +
  theme_bw() + #ggtitle("Attribute Ratings for Black and White Women's Names") +
  scale_shape_manual(values=c("Emily"=0, "Laurie"=1, "Misty"=2,
                              "Latoya"=15, "Keisha"=16, "Octavia"=17),
                     name="Name",
                     breaks=c("Emily", "Laurie", "Misty", "Latoya", "Keisha", "Octavia"),
                     labels=c("Emily"=expression(bold(Emily)), 
                              "Laurie"=expression(bold(Laurie)),
                              "Latoya"=expression(bold(Latoya)),
                              "Keisha"=expression(bold(Keisha)))) +
  ylab("Attribute") + xlab("Estimated Rating") +
  scale_y_discrete(breaks=c("White", "African American", "Competent", "Hardworking",
                            "Intelligent", "Professional", "Warm", "Working class")) 

pdf("Figures/figure3.pdf")
a
dev.off()

#clear environment
rm(list=ls())

## FIGURE 4 ####
dat <- read.csv(here("experiment_rawdata.csv"))
summarize <- dplyr::summarize

#create summary table of all allocations
a <- dat %>% group_by(tc) %>% summarize(mean(oppbucks, na.rm=T))
b <- dat %>% group_by(tc) %>% summarize(mean(embucks, na.rm=T))
c <- dat %>% group_by(tc) %>% summarize(mean(stbucks, na.rm=T))
d <- dat %>% group_by(tc) %>% summarize(sd(embucks, na.rm=T)/sqrt(n()))
f <- dat %>% group_by(tc) %>% summarize(sd(oppbucks, na.rm=T)/sqrt(n()))
g <- dat %>% group_by(tc) %>% summarize(sd(stbucks, na.rm=T)/sqrt(n()))
h <- dat %>% group_by(tc) %>% summarize(length(stbucks))
tab1 <- cbind(b[,2], a[,2], c[,2], d[,2], f[,2], g[,2], h[,2])
colnames(tab1) <- c("Emily $", "Other $", "State $", "SD Em", "SD Opp", "SD St", "N")
rownames(tab1) <- c("Excellent Keisha", "Excellent Laurie", "Excellent Misty",
                    "Excellent Octavia", "Poor Keisha", "Poor Laurie", "Poor Misty", "Poor Octavia")

#restructure to long for plotting purposes
a <- cbind(tab1$`Emily $`, tab1$`SD Em`, tab1$N, rownames(tab1), rep("em", 8))
a <- rbind(a, cbind(tab1$`Other $`, tab1$`SD Opp`, tab1$N, rownames(tab1), rep("op", 8)))
a <- rbind(a, cbind(tab1$`State $`, tab1$`SD St`, tab1$N, rownames(tab1), rep("st", 8)))
tab4 <- data.frame(a)
colnames(tab4) <- c("bucks", "sd", "n", "condition", "recipient")
tab4$bucks <- as.numeric(as.character(tab4$bucks))
tab4$sd <- as.numeric(as.character(tab4$sd))
tab4$n <- as.numeric(as.character(tab4$n))
tab4$recipient <- relevel(factor(tab4$recipient), ref="op")

#create subsets for plotting
pooronly <- c("Poor Keisha", "Poor Laurie", "Poor Misty", "Poor Octavia")
exonly <- c("Excellent Keisha", "Excellent Laurie", "Excellent Misty", "Excellent Octavia")

#change labels and orders
tab4$recipient <- factor(tab4$recipient, levels = c("em", "op", "st"))
tab4$condition <- factor(tab4$condition, levels= c("Excellent Keisha", "Excellent Laurie", "Excellent Misty",
                                                   "Excellent Octavia", "Poor Laurie","Poor Octavia", "Poor Keisha", "Poor Misty"))

#create plot
pdf(here("Figures/figure4.pdf"), width=12, height=5)
ggplot(data=tab4[tab4$condition %in% pooronly,]) + geom_bar(aes(x="op", y=bucks, fill=recipient), stat="identity", position="dodge", colour="black", width=3) +
  theme_bw() + facet_wrap(~condition, nrow=1) + 
  xlab("") +
  theme(axis.text.x=element_blank(), axis.ticks.x=element_blank()) + ylab("Average Dollars Allocated") +
  scale_fill_manual(name="Recipient", labels=c("Excellent Emily", "Treatment Name", "State"), values=c("grey38", "grey69", "gray100")) +
  geom_errorbar(aes(x=recipient, ymin=bucks-(1.96*(sd)), ymax=bucks+(1.96*(sd))), position="dodge", width=.5)
dev.off()


